package com.pocket.insight.base.context;

import com.alibaba.ttl.TransmittableThreadLocal;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/10
 */
public class OffsetLimitContext {

    private static TransmittableThreadLocal<Long> offsetInheritableThreadLocal = new TransmittableThreadLocal();
    private static TransmittableThreadLocal<Long> limitInheritableThreadLocal  = new TransmittableThreadLocal();
    private static TransmittableThreadLocal<String> dataFormatThreadLocal        = new TransmittableThreadLocal();

    public static String getDataFormat() {
        return (String)dataFormatThreadLocal.get();
    }

    public static void setDataFormat(String format) {
        dataFormatThreadLocal.set(format);
    }

    public static Long getOffset() {
        return (Long)offsetInheritableThreadLocal.get();
    }

    public static Long getOffset(Long defaultValue) {
        Long offset = getOffset();
        if (offset == null || offset.longValue() < 0L) {
            offset = defaultValue;
        }
        return offset;
    }

    public static Long getLimit() {
        return (Long)limitInheritableThreadLocal.get();
    }

    public static Long getLimit(Long defaultValue) {
        Long limit = getLimit();
        if (limit == null || limit.longValue() < 0L) {
            limit = defaultValue;
        }
        return limit;
    }

    public static void setOffset(Long offset) {
        offsetInheritableThreadLocal.set(offset);
    }

    public static void setLimit(Long limit) {
        limitInheritableThreadLocal.set(limit);
    }

    public static void remove() {
        dataFormatThreadLocal.remove();
        offsetInheritableThreadLocal.remove();
        limitInheritableThreadLocal.remove();
    }
}
